﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using CustomsDocumentOA.Project.CusDocument.Dto;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace CustomsDocumentOA.Project.CusDocument
{
    public class CustomerDetailAppservice : AsyncCrudAppService<CDOA_CustomerDetail, CustomerDetailDto, long, PagedResultRequestDto, UpdateCustomerDetailDto, UpdateCustomerDetailDto>, ICustomerDetailAppservice
    {
        private readonly IObjectMapper _objectMapper;
        public CustomerDetailAppservice(IRepository<CDOA_CustomerDetail, long> repository, IObjectMapper objectMapper) : base(repository)
        {
            _objectMapper = objectMapper;
        }

        #region 根据客户Id获取附件
        /// <summary>
        /// 根据客户Id获取附件
        /// </summary>
        /// <param name="customerId">客户主表Id,关联主表</param>
        /// <returns></returns>
        public async Task<List<CustomerDetailDto>> GetCustomerDetails(int customerId)
        {
            var list = await Repository.GetAllListAsync(o => o.cCustomerID == customerId);
            if (list.Count > 0)
            {
                return _objectMapper.Map<List<CustomerDetailDto>>(list);
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region 添加附件
        /// <summary>
        /// 添加附件
        /// </summary>
        /// <param name="customerDetailId">Id</param>
        /// <param name="fileAddress">附件地址</param>
        /// <returns></returns>
        public async Task<string> AddcFileAddress(long customerDetailId,string fileAddress) 
        {
            var obj = await Repository.FirstOrDefaultAsync(o => o.cCustomerID == customerDetailId);
            if (obj != null)
            {
                obj.cFileAddress = fileAddress;
                return "上传成功";
            }
            else 
            {
                return "上传失败";
            }
        }
        #endregion
    }
}
